<template>
	<view class="container">
		<view class="wrapper">
			<!-- <view style="text-align: center;">
				<image src="../../static/logo.png" style="border-radius: 64upx;"></image>
			</view>
 -->
			<view class="input-content">
				<view class="cu-form-group"
					style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<view class="title">手机号</view>
					<input type="number" v-model="userLoginVo.telephone" placeholder="请输入手机号" maxlength="11"
						data-key="telephone" @input="inputChange" />
				</view>
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;border-radius: 30px">
					<text class="title">验证码</text>
					<input type="number" v-model="userLoginVo.captcha" placeholder="请输入验证码" maxlength="6"
						data-key="code" @input="inputChange" @confirm="toLogin" />
					<button class="send-msg" @click="sendCaptcha" :disabled="sending">{{sendTime}}</button>
				</view>
			</view>
			<button class="confirm-btn" @click="toLogin">登录</button>
			<button class="confirm-btn" @click="toLoginPwd">账号密码登录</button>

			<view style="margin-top: 32px;text-align: center">
				<view><text style="color: #FFFFFF;font-size: 28upx;">没有账号？</text>
					<text style="color: #5E81F9" @click="register()">立即注册</text>
				</view>
			</view>
			<view class="footer">
				<text @tap="isShowAgree" class="cuIcon"
					:class="showAgree ? 'cuIcon-radiobox' : 'cuIcon-round'">同意</text>
				<!-- 协议地址 -->
				<navigator url="/my/setting/mimi" open-type="navigate" style="color: #5E81F9;">《隐私政策》</navigator>
				和
				<navigator url="/my/setting/xieyi" open-type="navigate" style="color: #5E81F9;">《用户服务协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userLoginVo: {
					telephone: '',
					captcha: ''
				},
				invitation: '',
				loginName: '',
				sending: false,
				sendTime: '获取验证码',
				count: 60,
				code: '',
				showAgree: false,
			}
		},
		onLoad() {

		},
		methods: {
			isShowAgree() {
				//是否选择协议
				this.showAgree = !this.showAgree;
			},
			forget() {
				uni.navigateTo({
					url: '/pages/public/forgetPwd'
				});
			},
			register() {
				uni.navigateTo({
					url: '/pages/public/register'
				});
			},
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack() {
				uni.navigateBack();
			},
			// 发送验证码
			sendCaptcha() {
				if (!this.userLoginVo.telephone) {
					uni.showToast({
						title: '请输入手机号',
						icon: "error"
					});
				} else if (!this.showAgree) {
					uni.showToast({
						title: '请先同意用户协议',
						icon: "error"
					})
				} else if (this.userLoginVo.telephone.length != 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: "error"
					});
				} else {
					this.$myRequest({
						url: '/sendCaptcha',
						data: {
							telephone: this.userLoginVo.telephone
						}
					}).then(r => {
						if (r.data.code == 200) {
							console.log(r);
							this.countDown();
						}
					})
				}
			},
			// 验证码倒计时
			countDown() {
				const {
					count
				} = this;
				if (count === 1) {
					this.count = 60;
					this.sending = false;
					this.sendTime = '获取验证码';
				} else {
					this.count = count - 1;
					this.sending = true;
					this.sendTime = count - 1 + '秒后重新获取';
					setTimeout(this.countDown.bind(this), 1000);
				}
			},
			// 登录
			toLogin() {
				if (!this.showAgree) {
					uni.showToast({
						title: '请先同意用户协议',
						icon: "error"
					})
				} else if (!this.userLoginVo.telephone) {
					uni.showToast({
						title: '请输入手机号',
						icon: "error"
					});
				} else if (this.userLoginVo.telephone.length != 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: "error"
					});
				} else if (!this.userLoginVo.captcha) {
					uni.showToast({
						title: '请输入验证码',
						icon: "error"
					});
				} else {
					this.$myRequest({
						url: '/login/byCaptcha',
						data: this.userLoginVo,
						method: 'post'
					}).then(r => {
						if (r.data.code == 200) {
							// 将用户id存储在本地缓存中
							uni.setStorageSync("userId", JSON.stringify(r.data.data));
							uni.showToast({
								title: "登录成功",
								icon: "success"
							})
							// 返回首页，关闭所有页面以重新刷新
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}
					});
				}
			},
			toLoginPwd() {
				uni.navigateTo({
					url: '/pages/public/login'
				})
			}
		}
	}
</script>

<style lang='scss'>
	.send-msg {
		border-radius: 30px;
		color: white;
		height: 30px;
		font-size: 14px;
		line-height: 30px;
		background: #5E81F9;
	}

	.container {
		top: 0;
		padding-top: 32upx;
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;
		background: #111224;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #111224;
		padding-bottom: 32upx;
	}


	.input-content {
		/* margin-top: 300upx; */
		/* padding-top: 300upx; */
		padding: 32upx 80upx;

	}


	.confirm-btn {
		width: 600upx;
		height: 80upx;
		line-height: 80upx;
		border-radius: 60upx;
		margin-top: 32upx;
		background: #5E81F9;
		color: #fff;
		font-size: 32upx;

		&:after {
			border-radius: 60px;
		}
	}

	.footer {
		padding-left: 140upx;
		margin-top: 32upx;
		text-align: center;
		display: flex;
		color: #FFFFFF;
	}
</style>
